package m.member.manage.service;

import m.common.model.util.ModelCheckUtil;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.service.Service;
import m.member.manage.model.MemberInfo;
import m.system.cache.CacheUtil;
import m.system.exception.MException;
import m.system.util.GenerateID;
import m.system.util.NumberUtil;
import m.system.util.ObjectUtil;
import m.system.util.StringUtil;
import manage.model.AdminLogin;
import manage.model.OrgGroupView;

import java.sql.SQLException;
import java.util.Date;

public class MemberInfoService extends Service {
    public String save(MemberInfo model, AdminLogin admin, OrgGroupView dept) throws MException, SQLException {
        ModelCheckUtil.checkNotNull(model,"realName","phoneNum","birthday","level.oid");
        if(!StringUtil.isMobileNO(model.getPhoneNum())){
            throw new MException(this.getClass(),"手机号格式错误");
        }
        ModelCheckUtil.checkUniqueCombine(model,new String[]{"phoneNum"},"该会员已存在");
        String msg;
        if (ObjectUtil.isEmptyOid(model)) {
            model.setOid(GenerateID.generatePrimaryKey());
            model.setInputDate(new Date());
            model.setInputAdmin(admin);
            model.setInputDept(dept);
            ModelUpdateUtil.insertModel(model);
            msg="保存成功";
        }else{
            ModelCheckUtil.equals(model,new String[]{"inputDept.oid"},new Object[]{dept.getOid()},"只有本店登记的才能修改");
            ModelUpdateUtil.updateModel(model,"realName","phoneNum","birthday","level.oid");
            msg="修改成功";
        }
        return msg;
    }

    /**
     * 更新冻结金额
     */
    public void updateFreeze(String oid,Double payAmount) throws Exception {
        MemberInfo member= ModelQueryList.getModel(MemberInfo.class,oid,"*");
        member.setFreeze(NumberUtil.round(member.getFreeze()+payAmount));
        ModelUpdateUtil.updateModel(member,"freeze");
        CacheUtil.clear(MemberInfo.class,oid);
    }

    /**
     * 更新金额和积分
     */
    public void updateAmount(String oid,Double realMoney,Double giveMoney,Integer point) throws Exception {
        MemberInfo member= ModelQueryList.getModel(MemberInfo.class,oid,"*");
        member.setRealMoney(NumberUtil.round(member.getRealMoney()+realMoney));
        member.setGiveMoney(NumberUtil.round(member.getGiveMoney()+giveMoney));
        member.setBalance(NumberUtil.round(member.getRealMoney()+member.getGiveMoney()));
        member.setPoint(member.getPoint()+point);
        ModelUpdateUtil.updateModel(member,"realMoney","giveMoney","balance","point");
        CacheUtil.clear(MemberInfo.class,oid);
    }

    /**
     * 添加订单数和已使用金额
     */
    public void addOrder(String oid,Double payAmount) throws Exception {
        MemberInfo member= ModelQueryList.getModel(MemberInfo.class,oid,"*");
        member.setOrderNum(member.getOrderNum()+1);
        member.setUseMoney(NumberUtil.round(member.getUseMoney()+payAmount));
        ModelUpdateUtil.updateModel(member,"orderNum","useMoney");
        CacheUtil.clear(MemberInfo.class,oid);
    }
}
